﻿namespace Yaaf.Wp7.AdvancedExplorer
{
    using System.Windows;

    using Microsoft.Phone.Controls;

    using System;

    using Microsoft.Phone.Shell;

    using Yaaf.Wp7.AdvancedExplorer.Resources;
    using Yaaf.Wp7.AdvancedExplorer.ViewModels;
    using Yaaf.WP7.NativeAccess.Registry;

    public partial class SetRegistryKeyWindow : PhoneApplicationPage
    {
        public SetRegistryKeyWindow()
        {
            InitializeComponent();


        }

        private bool translated = false;

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = new RegistryEntryEditViewModel(App.ViewModel.CurrentValueEditing);

            if (!translated)
            {
                // translate App menu
                AppMenuTranslatior.TranslateAppMenu(this.ApplicationBar, AppResources.ResourceManager);
                translated = true;
            }
        }

        private void saveClicked(object sender, EventArgs e)
        {
            var context = DataContext as RegistryEntryEditViewModel;
            if (context.Value == null)
            {
                MessageBox.Show("Please enter a value or release the inputbox (make the keyboard disappear)");
                return;
            }

            var regValue = new RegistryValue(context.KeyPath + "\\" + context.ValueName);
            try
            {
                switch (context.ValueType)
                {
                    case RegistryValueKind.DWord:
                        regValue.Value = int.Parse(context.Value);
                        break;
                    case RegistryValueKind.String:
                        regValue.Value = context.Value;
                        break;
                    case RegistryValueKind.Binary:
                        regValue.Value = Convert.FromBase64String(context.Value);
                        break;
                    case RegistryValueKind.MultiString:
                        regValue.Value = context.Value.Split('\n');
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not set value: " + ex, "Error", MessageBoxButton.OK);
            }
            
        }

        private void deleteClicked(object sender, EventArgs e)
        {
            var context = DataContext as RegistryEntryEditViewModel;
            var regValue = new RegistryValue(context.KeyPath + "\\" + context.ValueName);

            try
            {
                regValue.Value = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not set value: " + ex, "Error", MessageBoxButton.OK);
            }
        }

        private void loadClicked(object sender, EventArgs e)
        {
            var context = DataContext as RegistryEntryEditViewModel;
            var regValue = new RegistryValue(context.KeyPath + "\\" + context.ValueName);
            try
            {
                context.ValueType = regValue.ValueType;
                context.ValueObject = regValue.Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load value: " + ex, "Error", MessageBoxButton.OK);
            }
        }

        private void DeleteEnteredClick(object sender, EventArgs e)
        {
            var context = DataContext as RegistryEntryEditViewModel;
            try
            {
                Registry.DeleteKey(context.KeyPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not delete key: " + ex, "Error", MessageBoxButton.OK);
            }

            NavigationService.GoBack();
        }
    }
}